package com.finalterm.product.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.finalterm.product.entity.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 商品Mapper
 */
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
    
    /**
     * 扣减库存
     */
    @Update("UPDATE products SET stock = stock - #{quantity} WHERE id = #{productId} AND stock >= #{quantity}")
    int deductStock(@Param("productId") Long productId, @Param("quantity") Integer quantity);
    
    /**
     * 恢复库存
     */
    @Update("UPDATE products SET stock = stock + #{quantity} WHERE id = #{productId}")
    int recoverStock(@Param("productId") Long productId, @Param("quantity") Integer quantity);
}
